/*
 * ttbqt - A teletekst browser
 * http://code.google.com/p/ttbqt
 * Copyright (C) 2011, Kevin van der Vlist
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "ttdisp.h"

#include <QDebug>
#include <QMessageBox>

TTDisp::TTDisp(QMainWindow *ptr) {
  dock = new QDockWidget(QObject::tr("Teletekst pagina"), ptr);
  widget = new QWidget;

  // Remove unneeded stupid features:
  dock->setFeatures(QDockWidget::DockWidgetClosable);
  dock->setFeatures(QDockWidget::DockWidgetMovable);

  ttpagelabel = new QLabel(widget);

  layout = new QHBoxLayout;

  layout->addWidget(ttpagelabel);

  dock->setWidget(widget);

  widget->setLayout(layout);

  // To download the images
  qnam = new QNetworkAccessManager(this);
  QObject::connect(qnam, SIGNAL(finished(QNetworkReply*)), this, SLOT(imageFinished(QNetworkReply*)));
}

TTDisp::~TTDisp() {
  delete widget;
  delete dock;
  delete layout;
  delete ttpage;
  delete ttpagelabel;
  delete qnam;
}

QDockWidget *TTDisp::getWidget() {
  return dock;
}

// Connect the signal. Pointer to the window that has the SLOT function
void TTDisp::connect(Controller *ptr) {
  // Setup the signaling, set parent as receiver. 
  //QObject::connect(teleTekstBox, SIGNAL(currentIndexChanged(const QString &)), ptr, SLOT(teleTekstChannelChanged(const QString &)));
}

void TTDisp::setText(const QString &text) {
  ttpagelabel->setText(text);
}

void TTDisp::setTeletekstPage(const QString &url) {
  qDebug() << "setTeletekstPage(" << url << ");";
  QNetworkRequest request;
  request.setUrl(QUrl(url));
  request.setRawHeader("User-Agent", TTBQT_UA);
  qnam->get(request);
}

void TTDisp::imageFinished(QNetworkReply* reply) {
  qDebug() << "imageFinished() - url: " << reply->request().url();
  ttpagelabel;
  QImage image;
  if (reply->error() == QNetworkReply::NoError) {
    // Valid reply, display it
    image.load(reply, 0);
    if (!image.isNull()) {
      ttpagelabel->setPixmap(QPixmap::fromImage(image));
      qDebug() << "Run was correct";
    }
  }
  else {
    // Page not found
      qDebug() << "Run was incorrect";
      ttpagelabel->setText(tr("Teletekst page does not exist."));
  }
  reply->deleteLater();
}

void TTDisp::displayHelp() {
  // Bind the messagebox to the parent widget of the dock, the mainwindow
  QString controls = QString("Bediening:\nVolgende pagina \t %1\nVorige pagina \t %2\nVolgende subpagina \t %3\nVorige subpagina \t %4\nAfsluiten \t\t %5").arg("b").arg("f").arg("n").arg("p").arg("q");
  QString pchoice = QString("Direct naar pagina 123 gaan kan door de toetsen [spatie][1][2][3] in te drukken");
  QMessageBox::about( dock->parentWidget(), BINARY, QString("%1\nVersie %2\n\n%3\n%4\n\n%5\n\n%6").arg(TTBQT_UA).arg(TTBQT_VERSION).arg(TTBQT_COPYRIGHT).arg(TTBQT_URL).arg(controls).arg(pchoice));
}
